package com.fangcun.service.api.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangcun.bean.api.goods.request.GoodsSearchRequest;
import com.fangcun.bean.api.goods.response.GoodsDetailResponse;
import com.fangcun.bean.api.goods.response.GoodsSearchResponse;
import com.fangcun.bean.sys.PageBean;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.exception.ProValiDataException;
import com.fangcun.common.util.base.BaseService;
import com.fangcun.mapper.GoodsMapper;
import com.fangcun.service.api.GoodsApiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @作者： 陳曉鬆 @时间： 2020年7月15日 - 上午11:21:59 @description：->
 */
@Service("goodsApiService")
public class GoodsApiServiceImpl extends BaseService implements GoodsApiService {

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public PageInfo<GoodsSearchResponse> list(PageBean<GoodsSearchRequest> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<GoodsSearchResponse> list = goodsMapper.listForApi(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public GoodsDetailResponse detail(Long priceId, Long userId) throws ProException {
		GoodsDetailResponse response = goodsMapper.detailForApi(priceId, userId);
		if (response == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		try {
			Object monthNumber = redisService.get(15, "MONTH_LYORDER_QUANTITY");
			if (!org.springframework.util.StringUtils.isEmpty(monthNumber)) {
				response.setMonthlyOrderQuantity(response.getMonthlyOrderQuantity() + Integer.valueOf(monthNumber.toString()));
			}
		} catch (Exception e) {
		}
		return response;
	}

}
